﻿// -----------------------------------------------------------------------
// <copyright file="RankPhotos.cs" company="Hewlett-Packard">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex3.Logic
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using FacebookWrapper.ObjectModel;

    /// <summary>
    /// Represents the rank photos collection
    /// </summary>
    public class RankPhotos : IEnumerable<RankedPhoto>
    {
        private Dictionary<string, RankedPhoto> Photos { get; set; }

        public RankPhotos()
        {
            this.Photos = new Dictionary<string, RankedPhoto>();
        }

        public RankedPhoto this[string i_Index]
        {
            get { return this.Photos[i_Index]; }
        }

        public bool ContainsID(string i_ID)
        {
            return this.Photos.ContainsKey(i_ID);
        }

        public RankedPhoto GetPhotoByID(string i_ID)
        {
            if (!this.ContainsID(i_ID))
            {
                return null;
            }
            else
            {
                return this.Photos[i_ID];
            }
        }

        public void AddPhoto(RankedPhoto i_Photo)
        {
            this.Photos.Add(i_Photo.ID, i_Photo);
        }

        public RankedPhoto GetPhotoByURL(string i_Url)
        {
            RankedPhoto returnPhoto = null;

            foreach (RankedPhoto currentPhoto in this.Photos.Values)
            {
                if (string.Equals(currentPhoto.URL, i_Url))
                {
                    returnPhoto = currentPhoto;
                }
            }

            return returnPhoto;
        }

        public void Rank(Photo i_Photo, Common.Stars i_Stars)
        {
            RankedPhoto photo = new RankedPhoto(i_Photo, i_Stars);
            if (this.Photos.ContainsKey(i_Photo.Id))
            {
                this.Photos[i_Photo.Id].RankedStars = i_Stars;
            }
            else
            {
                this.Photos.Add(i_Photo.Id, photo);
            }
        }

        /// <summary>
        /// Loades from the files and translates the photo from fileRank
        /// </summary>
        /// <param name="i_User">The user</param>
        public void Load(User i_User)
        {
            FileRankPhotos fileRank = new FileRankPhotos();
            fileRank.Load(i_User);

            foreach (FileRank ranked in fileRank.FilePhotos)
            {
                RankedPhoto photo = new RankedPhoto(ranked.Url, ranked.Id, (Common.Stars)ranked.NumberOfStars);
                if (photo != null)
                {
                    this.Photos.Add(photo.ID, photo);
                }
            }
        }

        public IEnumerator<RankedPhoto> GetEnumerator()
        {
            foreach (RankedPhoto currentPhoto in this.Photos.Values)

            {
                yield return currentPhoto;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}
